﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PulseApp
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Page
    {


        public Login()
        {
            InitializeComponent();

        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            
            string username = UsernameBox.Text;
            string password = PasswordBox.Password;

            using (DbEntities db = new DbEntities())
            {
                var users = from u in db.t_person
                            where u.username.Equals(username) && u.password.Equals(password)
                            select u;
                if (!users.Any())
                {
                    messageLabel.Text = "We can't log you in. Please check you username , password and try again.";
                }
                else
                {
                    Person aPerson = new Person(users.FirstOrDefault());
                    CommonResources.Instance.setPerson(aPerson);
                    CommonResources.Instance.setRole(aPerson.Role);
                    NavigationService.Navigate(new Uri("Index.xaml", UriKind.Relative));
                }
            }

            
        }

        private void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                LoginButton_Click(sender, e);
            }
        }
    }
}
